﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;

namespace CSharp.Infrastructure
{
    /// <summary>
    /// 服务扩展
    /// </summary>
    public static class ServiceExtendHelper
    {
        /// <summary>
        /// 条件为true时，附加查询条件
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="baseQuery"></param>
        /// <param name="val">bool?</param>
        /// <param name="where"></param>
        /// <returns></returns>
        public static IQueryable<T> CheckIfNotEmpty<T>(this IQueryable<T> baseQuery, string val, Expression<Func<T, bool>> where)
        {
            if (string.IsNullOrEmpty(val))
            {
                return baseQuery;
            }

            return baseQuery.Where(where);
        }

        /// <summary>
        /// val不为空时，附加查询条件
        /// </summary>
        /// <param name="baseQuery"></param>
        /// <param name="is_true"></param>
        /// <param name="where"></param>
        /// <returns></returns>
        public static IQueryable<T> CheckWhere<T>(this IQueryable<T> baseQuery, bool is_true, Expression<Func<T, bool>> where)
        {
            if (is_true)
            {
                return baseQuery.Where(where);
            }

            return baseQuery;
        }
    }
}
